<?php
/**
 * GameModel
 * 
 * @author Admin
 * @version 
 */
require_once 'Zend/Db/Table/Abstract.php';
class Wap_Models_TblNoteModel extends Zend_Db_Table_Abstract
{
    /**
     * The default table name 
     */
    protected $_name = 'tbl_note';
    protected $_primary = 'idnote';
    protected $tbl_useractivity = 'useractivity';

    function getListNote($page=1){
        $db = Zend_Db_Table::getDefaultAdapter();
        $mySql = $db->select()
                    ->from($this->_name)
                    ->where('active = 1')
                    ->order('idnote desc')
                    ->limitPage($page, 5);
    //echo $mySql;die;
        $result = $db->fetchAll($mySql);
        return $result;
    }

    function getListNoteNotIdView($id, $gid){
        $db = Zend_Db_Table::getDefaultAdapter();
        $mySql = $db->select()
                    ->from($this->_name)
                    ->where('active = 1 and idnote !=?', $id)
                    ->where('GroupNoteID =?', $gid)
                    ->order('idnote desc')
                    ->limit(10);
    //echo $mySql;die;
        $result = $db->fetchAll($mySql);
        return $result;
    }

    function getCountAllNote(){
        $db = Zend_Db_Table::getDefaultAdapter();
        $mySql = $db->select()
                    ->from(array('p'    => $this->_name),array('count_note' => 'count(*)'))
                    ->where('active = 1');

        $result = $db->fetchRow($mySql);
        return $result['count_note'];
    }

    function getListMostNote($page=1){
        $db = Zend_Db_Table::getDefaultAdapter();
        $mySql = $db->select()
                    ->from($this->_name)
                    ->where('active = 1')
                    ->order('view desc')
                    ->limitPage($page, 10);
    //echo $mySql;die;
        $result = $db->fetchAll($mySql);
        return $result;
    }    

    function getNoteByCategory($groupID=0, $page=1){
        $db = Zend_Db_Table::getDefaultAdapter();
        $mySql = $db->select()
                    ->from($this->_name)
                    ->where('active = 1')
                    ->order('idnote desc')
                    ->limitPage($page, 10);
    if($groupID!=0) $mySql->where(' GroupNoteID='.$groupID);                    
    //echo $mySql;die;
        $result = $db->fetchAll($mySql);
        return $result;
    }

    function getCountNoteByCategory($groupID=0){
        $db = Zend_Db_Table::getDefaultAdapter();
        $mySql = $db->select()
                    ->from(array('p'    => $this->_name),array('count_note' => 'count(*)'))
                    ->where('active = 1');
        if($groupID!=0) $mySql->where(' GroupNoteID='.$groupID);

        $result = $db->fetchRow($mySql);
        return $result['count_note'];
    }
    
    function getCountChap($idpart){
        $db = Zend_Db_Table::getDefaultAdapter();
        $mySql = $db->select()
                    ->from(array('p'    => $this->_name),array('count_chap' => 'count(*)'))
                    ->where('active = 1')
                    ->order('idnote desc');

        $result = $db->fetchRow($mySql);
        return $result['count_chap'];
    }

    function getListChap($idpart, $page=1){
        $db = Zend_Db_Table::getDefaultAdapter();
        $mySql = $db->select()
                    ->from($this->_name)
                    ->where('active = 1')
                    ->order('idnote asc')
                    ->limitPage($page, 10);
    //echo $mySql;die;
        $result = $db->fetchAll($mySql);

        return $result;
    }

    function getNoteGroup($groupID){
        $db = Zend_Db_Table::getDefaultAdapter();
        $mySql = $db->select()
                    ->from($this->_name)
                    ->where('GroupNoteID=?', $groupID) ;   
    //echo $mySql;die;
        $result = $db->fetchAll($mySql);

        return $result;
    }

    function getNoteID($idnote){
        $db = Zend_Db_Table::getDefaultAdapter();
        $mySql = $db->select()
                    ->from($this->_name)
                    ->where('idnote=?', $idnote) ;   
    //echo $mySql;die;
        $result = $db->fetchRow($mySql);

        return $result;
    }

    function getCategoryNoteID($gid){
        $db = Zend_Db_Table::getDefaultAdapter();
        $mySql = $db->select()
                    ->from('tbl_groupnote')
                    ->where('GroupNoteID=?', $gid) ;   
    //echo $mySql;die;
        $result = $db->fetchRow($mySql);

        return $result;
    }

    function getNextNoteLoadID($idCatNote, $idnote){
        $db = Zend_Db_Table::getDefaultAdapter();
        $mySql = $db->select()
                    ->from($this->_name)
                    ->where('idnote>'.$idnote)
                    ->order('idnote asc');   
    //echo $mySql;die;
        $result = $db->fetchRow($mySql);

        return $result['idnote'];
    }
    
    function getPveNoteLoadID($idCatNote, $idnote){
        $db = Zend_Db_Table::getDefaultAdapter();
        $mySql = $db->select()
                    ->from($this->_name)
                    ->where('idnote<'.$idnote)
                    ->order('idnote desc');   
    //echo $mySql;die;
        $result = $db->fetchRow($mySql);

        return $result['idnote'];
    }

    public function updateViewNote($data, $idnote){
//      Load model và form sử dụng trong action
        Zend_Loader::loadClass('Wap_Models_Main');     
          
        $mMain = new Wap_Models_Main();
        $result = $mMain->editTable ( $this->_name, $data, "idnote", $idnote );
        return $result;
    }


}